import Login from '@Page/login'
import { h } from 'vue'
import User from '@Page/user'
import { IconYunYingGuanLi } from '@Assets/fonts/svg'
import { useRoute, useRouter } from 'vue-router'
import Main from '@Page/user/Main'
import roleMenu from '@Routers/role'
import recycleBinMenu from '@Routers/recycleBin'
import { getUserInfo } from '@Config/server/localStorage'
import { findIndex, flatMapDeep } from 'lodash'
import { clearChildren, deleteChildren } from '@Untils'

export default () => {
  let base_router = [
    {
      path: '/',
      component: Login,
      meta: { disableInMenu: true },
      name: 'root',
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'NotFound',
      component: {
        template: `<h1>err</h1>`,
        setup() {
          let router = useRouter()
          let route = useRoute()
          let path = route.path
          setTimeout(() => {
            router.replace({ path: '/sys' })
          }, 1)
        },
      },
      meta: { disableInMenu: true },
    },
    {
      path: '/sys',
      icon: h(IconYunYingGuanLi),
      component: <Main />,
      meta: { cn: '高速网盘', onlyNode: true },
      name: 'sys',
      // //redirect: '/sys/management',
      children: [],
    },
  ]
  if (getUserInfo()?.token) {
    let _index = findIndex(base_router, { name: 'sys' })
    if (
      base_router[_index].children &&
      base_router[_index].children instanceof Array
    ) {
      base_router[_index].children?.push(powerList())
    } else {
      base_router[_index].children = [powerList()]
    }
  } else {
    console.log('未登录')
  }

  // let g = {
  //   path: 'management',
  //   icon: h(IconYunYingGuanLi),
  //   component: <User />,
  //   meta: { cn: '系统管理' },
  //   name: 'management',
  //   //redirect: '/sys/management/categorymanagement/first',
  //   children: [
  //     statisticsMenu(),
  //     categoryMenu(), /////
  //     orderMenu(), ////
  //     commoditymanagementMenu(),
  //     markMenu(),
  //     supplyordeerMenu(), //供货单
  //     suppliermanagerMenu(),
  //     cashoutMenu(),
  //     productMenu(),
  //     couponMenu(),
  //     checkMenu(),
  //     systemMenu(),
  //     roleMenu(),
  //   ].filter((x) => x != null),
  // }

  // clearChildren(g)

  // let len = g.children.length
  // for (var i = 0; i < len; i++) {
  //   deleteChildren(g)
  // }
  // console.log(g)

  //  console.log(base_router[base_router.length - 1])

  // console.log(
  //   JSON.stringify(
  //     flatMapDeep(makeConfigJson(base_router[base_router.length - 1]))
  //   )
  // )

  // console.log(JSON.stringify(flatMapDeep(makeConfigJson(base_router[1]))))
  // console.log('-------------------------------------------------------------')
  return base_router
}

export const powerList = () => {
  // console.log('0000000000000')
  let _result = {
    path: 'management',
    icon: h(IconYunYingGuanLi),
    component: <User />,
    meta: { cn: '网盘管理' },
    name: 'management',
    //redirect: '/sys/management/categorymanagement/first',
    children: [recycleBinMenu(), roleMenu()].filter((x) => x != null),
  }
  // console.log('result', _result)
  let _filter_menu_list = _result
  clearChildren(_filter_menu_list)

  let len = _filter_menu_list.children.length
  for (var i = 0; i < len; i++) {
    deleteChildren(_filter_menu_list)
  }
  //console.log('result2', _filter_menu_list)

  return _filter_menu_list
}

const makeConfigJson = (_menu) => {
  return _menu.children.map((x) => {
    if (x.children) {
      return makeConfigJson(x)
    } else {
      return {
        cn: x.meta.cn,
        name: x.name,
        groupName: _menu.name,
        canBeIndex: !x.meta.disableInMenu,
        groupTagName: _menu.meta.cn,
        btns: x.meta.btns,
      }
    }
  })
}
/**
 *
 * @param {*} _root_name
 * @param {*} sys
 * @param {*} root
 * @description 在主页面中设置路由加载 检测方法
 */
export const hooks = (
  _root_name = 'management',
  sys = 'sys',
  root = 'root'
) => {
  let router = useRouter()
  let route = useRoute()
  let flag = getUserInfo()?.permissionTagList || []

  let _power_list = powerList()
  if (!router.hasRoute(_root_name) && _power_list.children.length > 0) {
    router.addRoute(sys, _power_list)
  }

  if (flag.indexOf(route.name) >= 0) {
    //从login登录，需要再次添加路由
    if (route.name == sys) {
      router.push({ name: flag[0] })
    } else {
      router.push({ ...route })
    }
  } else {
    if (flag.length > 0) {
      router.push({ name: flag[0] })
    } else {
      router.push({ name: root })
    }
  }
}
